import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:infinite_scroll_pagination/infinite_scroll_pagination.dart';
import 'package:pornhub_app/components/app_bg_view.dart';
import 'package:pornhub_app/components/base_refresh/base_refresh_widget.dart';
import 'package:pornhub_app/components/no_more/no_data.dart';
import 'package:pornhub_app/components/text_view.dart';
import 'package:pornhub_app/generate/app_image_path.dart';
import 'package:pornhub_app/model/mine/record_model.dart';
import 'package:pornhub_app/utils/color.dart';
import 'package:pornhub_app/utils/utils.dart';

import 'recharge_record_page_controller.dart';

class RechargeRecordPage extends GetView<RechargeRecordPageController> {
  const RechargeRecordPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: COLOR.color_161616,
      appBar: _buildAppBar(),
      body: _buildContentView(),
    );
  }

  AppBar _buildAppBar() {
    return AppBar(
      backgroundColor: COLOR.color_161616,
      leading: IconButton(
        iconSize: 20.w,
        icon: const Icon(
          Icons.arrow_back_ios,
          color: COLOR.white,
        ),
        onPressed: () => Get.back(),
      ),
      title: TextView(
        text: "充值记录",
        color: COLOR.white,
        fontSize: 16.w,
        fontWeight: FontWeight.w600,
      ),
    );
  }

  Widget _buildContentView() {
    return EasyRefresh(
      controller: controller.refreshController,
      refreshOnStart: true,
      onRefresh: () => controller.onRefresh(),
      onLoad: () => controller.onLoad(),
      child: PagedListView<int, RecordModel>(
        padding: EdgeInsets.only(top: 10, left: 14.w, right: 14.w),
        addAutomaticKeepAlives: true,
        pagingController: controller.pagingControllers,
        builderDelegate: PagedChildBuilderDelegate<RecordModel>(
          firstPageProgressIndicatorBuilder: (context) =>
              const SizedBox.shrink(),
          newPageProgressIndicatorBuilder: (context) => const SizedBox.shrink(),
          noMoreItemsIndicatorBuilder: (context) => const SizedBox.shrink(),
          noItemsFoundIndicatorBuilder: (context) => const NoData(),
          itemBuilder: (context, item, index) => _buildItemView(item),
        ),
      ),
    );
  }

  _buildItemView(RecordModel model) {
    return AppBgView(
      backgroundColor: COLOR.color_252525,
      width: double.infinity,
      margin: EdgeInsets.only(bottom: 10.w),
      padding: EdgeInsets.symmetric(horizontal: 18.w, vertical: 12.w),
      radius: 6.w,
      child: Row(
        children: [
          Image.asset(
            controller.getPayType(model.payType ?? 0),
            width: 40.w,
            height: 40.w,
          ),
          10.horizontalSpace,
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Row(
                  children: [
                    TextView(
                      text: model.desc ??
                          controller.getTranType(model.tranType ?? 0),
                      color: COLOR.white,
                      fontSize: 16.sp,
                    ),
                    const Spacer(),
                    TextView(
                      text: "¥${model.amount}",
                      color: COLOR.white,
                      fontSize: 20.w,
                    ),
                  ],
                ),
                5.verticalSpace,
                SizedBox(
                  width: double.infinity,
                  child: TextView(
                    text: '订单编号：${model.tradeNo}',
                    color: COLOR.color_AFAFAF,
                    fontSize: 11.sp,
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    textAlign: TextAlign.start,
                  ),
                ),
                5.verticalSpace,
                SizedBox(
                  width: double.infinity,
                  child: TextView(
                    text: Utils.dateFmt(
                        model.createdAt!, ["yyyy", "-", "mm", "-", "dd"]),
                    color: COLOR.color_AFAFAF,
                    fontSize: 12.w,
                    textAlign: TextAlign.start,
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
